//
//  YTNumPad.m
//  KoalaPad
//
//  Created by 张英堂 on 15/4/7.
//  Copyright (c) 2015年 zyt. All rights reserved.
//

#import "YTNumPad.h"
#import "UIColor+Mycolor.h"

#define KPADBTNTAG 100

@implementation YTNumPad


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor ytColorWithRed:199 green:204 blue:211 alpha:1]];
        [self creatNumPad];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    [self creatNumPad];
}

- (void)creatNumPad{
    NSArray *array = @[@[@"1", @"2", @"3"],
                       @[@"4", @"5", @"6"],
                       @[@"7", @"8", @"9"],
                       @[@"←", @"0", @"完成"]];
    
    CGFloat height = self.frame.size.height*0.96;
    CGFloat width = self.frame.size.width*0.96;

    for (int i = 0; i < 4; i++) {
        for (int y = 0; y < 3; y++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            CGRect rect = CGRectMake(width/3*y+width/3*0.03+self.frame.size.width*0.02, height/4*i+height/4*0.05+self.frame.size.height*0.02, width/3*0.94, height/4*0.9);
            
            [button setFrame:rect];
            NSString *title = array[i][y];
            
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor clearColor]];
            [button.titleLabel setFont:[UIFont systemFontOfSize:height / 4 * 0.3]];
            [button setShowsTouchWhenHighlighted:YES];
            
            button.tag = KPADBTNTAG + i * 3 + y;
            
            UIImage *btnImage = nil;
            if (button.tag == PadNum_Delete+KPADBTNTAG || button.tag == PadNum_Return+KPADBTNTAG) {
                btnImage = [UIImage imageNamed:@"rt_btn"];
            }else{
                btnImage = [UIImage imageNamed:@"num_btn"];
            }
            [button setBackgroundImage:btnImage forState:UIControlStateNormal];
            
            if (button.tag == PadNum_Delete+KPADBTNTAG) {
                UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(rect.size.width*0.32, rect.size.height*0.32, rect.size.width*0.34, rect.size.height*0.34)];
                [backImage setContentMode:UIViewContentModeScaleAspectFit];
                [backImage setImage:[UIImage imageNamed:@"btn_back"]];
                [button addSubview:backImage];
                [button setTitle:@"" forState:UIControlStateNormal];
            }
            
            [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
        }
    }
}

- (void)click:(UIButton *)button{
    
    if (self.delegate) {
        NSInteger btn_tag = button.tag - KPADBTNTAG;
        
        switch (btn_tag) {
            case PadNum_Delete:
            {
                [self.delegate numPadClickDelete:self];
            }
                break;
            case PadNum_Return:
            {
                [self.delegate numPadClickReturn:self];
            }
                break;
            case PadNum_Zero:
            {
                [self.delegate numPad:self clickNum:0];
            }
                break;
            default:
            {
                [self.delegate numPad:self clickNum:btn_tag + 1];
            }
                break;
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
